package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import common.ServerResponse;
import service.ICategoryService;
import service.impl.CategoryServiceImpl;

@WebServlet("/manage/category")
public class CategoryServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		setHead(request, response);
		String type=request.getParameter("type");
		if("get_category".equals(type)) {
			get_category(request,response);
		}
		else if("add_category".equals(type)) {
			add_category(request,response);
		}
		else if("set_category_name".equals(type)) {
			set_category_name(request,response);
		}
		else if("get_deep_category".equals(type)) {
			get_deep_category(request,response);
		}
	
	
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	//操作区////////////
	
	//获取品类子节点（平级）
	protected void get_category(HttpServletRequest request, HttpServletResponse response) {
		//获取登录用户的身份
				//HttpSession session = request.getSession();
				//Object Obj_role = session.getAttribute("role");
				//String role = Obj_role.toString();
		        String role ="0";
				//获取品类id
				String category_id=request.getParameter("category_id");
				//执行获取逻辑
				ICategoryService categoryService= new CategoryServiceImpl();
				ServerResponse se = categoryService.Get_category(category_id,role);
				//将ServerResponse转为json并通过流写出
				getjson(se,response);
		
	}
	
	//添加品类
	protected void add_category(HttpServletRequest request, HttpServletResponse response) {
		String parent_id=request.getParameter("parent_id");
		String categoryName=request.getParameter("categoryName");
		System.out.println("打印数据"+parent_id+" "+categoryName);
		ICategoryService c=new CategoryServiceImpl();
		getjson(c.Add_category(parent_id, categoryName),response);
	}
	
	//修改品类名称
	protected void set_category_name(HttpServletRequest request, HttpServletResponse response) {
		String categoryid=request.getParameter("category_id");
		String category_name=request.getParameter("category_name");
		ICategoryService c=new CategoryServiceImpl();
		getjson(c.Set_category_name(categoryid, category_name),response);
		
	}
	
	//获取当前分类id及递归子节点categoryId
	//递归获取父类节点直到没有父类（他的parentid=0）
	protected void get_deep_category(HttpServletRequest request, HttpServletResponse response) {
		String category_id=request.getParameter("category_id");
		ICategoryService c=new CategoryServiceImpl();
		getjson(c.Get_deep_category(category_id),response);
	}
	//数据转成json
	protected void getjson(ServerResponse se,HttpServletResponse response) {
		Gson gson=new Gson();
		String json=gson.toJson(se);
		PrintWriter pw;
		try {
			pw=response.getWriter();
			pw.write(json);
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	//设置头信息
    protected void setHead(HttpServletRequest request,HttpServletResponse response) {
    	//转发时候防止中文乱码
	    response.setHeader("content-type", "text/html;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    String originHeader=request.getHeader("Origin");
		/* 允许跨域的主机地址 */ response.setHeader("Access-Control-Allow-Origin", originHeader);
		/* 允许跨域的请求方法GET, POST, HEAD 等 */ response.setHeader("Access-Control-Allow-Methods", "*"); 
		/* 重新预检验跨域的缓存时间 (s) */ response.setHeader("Access-Control-Max-Age", "3600");
		/* 允许跨域的请求头 */ response.setHeader("Access-Control-Allow-Headers", "*"); 
		/* 是否携带cookie */ response.setHeader("Access-Control-Allow-Credentials", "true");
		
		
	}
	
	
	

}
